package com.daocao.jy.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.jy.R;
import com.daocao.jy.api.Api;
import com.daocao.jy.api.bean.OilcardAddBean;
import com.daocao.jy.api.bean.OilcardEditBean;
import com.daocao.jy.api.model.OilcardListModel;
import com.daocao.jy.base.BaseActivity;
import com.daocao.jy.util.Network;
import com.daocao.jy.util.RxLifeUtil;
import com.daocao.jy.util.ToastUtil;
import com.daocao.jy.util.UI;
import com.daocao.jy.util.Util;
import com.daocao.jy.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class OilCardAddActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_shihua_check)
    ImageView imgShihuaCheck;
    @BindView(R.id.btn_shihua)
    LinearLayout btnShihua;
    @BindView(R.id.img_shiyou_check)
    ImageView imgShiyouCheck;
    @BindView(R.id.btn_shiyou)
    LinearLayout btnShiyou;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.edit_oil_card)
    EditText editOilCard;
    @BindView(R.id.edit_oil_card_sec)
    EditText editOilCardSec;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.btn_agency)
    TextView btnAgency;
    @BindView(R.id.txt_tips_one)
    TextView txtTipsOne;
    @BindView(R.id.txt_tips_two)
    TextView txtTipsTwo;

    private Unbinder unbiner;
    private String selectType = "";
    private boolean isEdit = false;
    private static String SHI_YOU;
    private static String SHI_HUA;
    private OilcardListModel.DatasBean oil_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_oil_card_add);
        initWebViewBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {

        isEdit = getIntent().getBooleanExtra("is_edit", false);
        oil_data = getIntent().getParcelableExtra("oil_data");
        if (isEdit) {
            txtTipsOne.setText("重新选择加油卡类型");
            txtTipsTwo.setText("编辑其他信息");
            btnAgency.setVisibility(View.GONE);
            btnSubmit.setText("保存");
            txtRight.setVisibility(View.GONE);
            txtTitle.setText("编辑我的加油卡");
            editName.setText(oil_data.name);
            editPhone.setText(oil_data.phone);
            editOilCard.setText(oil_data.card_number);
            editOilCardSec.setText(oil_data.card_number);
        } else {
            txtTitle.setText("添加加油卡");
            txtRight.setVisibility(View.VISIBLE);
            txtRight.setText("办加油卡");
        }
        getOilType();
    }


    public void getOilType() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        Api.api_service.getOilCardType().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.datas != null && result.datas.size() != 0) {
                        for (int i = 0; i < result.datas.size(); i++) {
                            if (result.datas.get(i).name.equals("中国石油")) {
                                SHI_YOU = result.datas.get(i).id;
                            } else if (result.datas.get(i).name.equals("中国石化")) {
                                SHI_HUA = result.datas.get(i).id;
                            }
                            if (isEdit) {
                                if (oil_data.ctype.equals(SHI_YOU)) {
                                    selectType = SHI_YOU;
                                    imgShihuaCheck.setImageResource(R.mipmap.img_oil_uncheck);
                                    imgShiyouCheck.setImageResource(R.mipmap.img_oil_check);
                                } else if (oil_data.ctype.equals(SHI_HUA)) {
                                    selectType = SHI_HUA;
                                    imgShihuaCheck.setImageResource(R.mipmap.img_oil_check);
                                    imgShiyouCheck.setImageResource(R.mipmap.img_oil_uncheck);
                                }
                            }
                        }
                    }
                });
    }

    public void getOilEdit(String id, String card_number, String ctype_id, String name, String phone) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        OilcardEditBean bean = new OilcardEditBean();
        bean.id = id;
        bean.card_number = card_number;
        bean.ctype_id = ctype_id;
        bean.name = name;
        bean.phone = phone;
        Api.api_service.getOilcardEdit(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "修改成功");
                });
    }

    public void getOilAdd(String card_number, String ctype_id, String name, String phone) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        OilcardAddBean bean = new OilcardAddBean();
        bean.card_number = card_number;
        bean.ctype_id = ctype_id;
        bean.name = name;
        bean.phone = phone;
        Api.api_service.getOilcardAdd(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "添加成功");
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.txt_right, R.id.btn_back, R.id.btn_shihua, R.id.btn_shiyou, R.id.btn_submit, R.id.btn_agency})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_shihua:
                if (TextUtils.isEmpty(SHI_HUA)) {
                    getOilType();
                    return;
                }
                if (selectType.equals(SHI_HUA)) {
                    return;
                }
                selectType = SHI_HUA;
                imgShihuaCheck.setImageResource(R.mipmap.img_oil_check);
                imgShiyouCheck.setImageResource(R.mipmap.img_oil_uncheck);
                break;
            case R.id.btn_shiyou:
                if (TextUtils.isEmpty(SHI_YOU)) {
                    getOilType();
                    return;
                }
                if (selectType.equals(SHI_YOU)) {
                    return;
                }
                selectType = SHI_YOU;
                imgShiyouCheck.setImageResource(R.mipmap.img_oil_check);
                imgShihuaCheck.setImageResource(R.mipmap.img_oil_uncheck);
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(selectType)) {
                    ToastUtil.makeText(this, "请选择油卡类型");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editPhone))) {
                    ToastUtil.makeText(this, "请输入手机号");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editOilCard))) {
                    ToastUtil.makeText(this, "请输入油卡卡号");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editOilCardSec))) {
                    ToastUtil.makeText(this, "请再次确认油卡卡号");
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editName))) {
                    ToastUtil.makeText(this, "请输入您的姓名");
                    return;
                }
                if (isEdit) {
                    getOilEdit(oil_data.id, UI.toString(editOilCard), selectType, UI.toString(editName), UI.toString(editPhone));
                } else {
                    getOilAdd(UI.toString(editOilCard), selectType, UI.toString(editName), UI.toString(editPhone));
                }
                break;
            case R.id.txt_right:
                Util.startActivity(this, OilAgencyActivity.class);
                break;
            case R.id.btn_agency:
                Util.startActivity(this, OilAgencyActivity.class);
                break;
        }
    }
}
